<?php
namespace Mentors\Controller;

class ApprenticeController extends MainController {

    public function index(){
        $status = I('get.status');
        if($status!=""){
           $this->assign('status', $status);
           if($status!=-1){
              $map['status']=$status;
           }
        }else{
          $this->assign('status', -1);  
        }
        $obj = M('Apprentice');
        $map['toID']=$this->userID;
        $count = $obj->where($map)->count();
        import("Common.ORG.Page");
        $page = new \Page($count, 10);
        $show = $page->show();
        $list = $obj->where($map)->order('id desc')->limit($page->firstRow . ',' . $page->listRows)->select();
        $this->assign('list', $list);
        $this->assign('page', $show);
        $this->display();
    }   

    public function view(){
        $id = (int)I('get.id');
        $obj = M('Apprentice');
        $map['toID']=$this->userID;
        $map['id']=$id;
        $list = $obj->where($map)->find();

        if (!$list) {
            $this->error(L('_public_noinfo'));
        } else {
            $this->assign('list',$list);
            $this->display();
        }
    }

    public function save(){
        $obj = M('Apprentice');
        $map['toID'] = $this->userID;
        $map['id'] = I('post.id');      
        $info = $obj->where($map)->find();  
        if (!$info) {
            $state = L('_public_noinfo');
            echo $this->echo_json_str($state,$message,$url); die;
        }  

        $data['status'] = I('post.status');
        $data['remark'] = I('post.remark');
        $data['updateTime'] = time(); 
        if ($data['status']=='') {
            $state = 'Please choose the status';
            echo $this->echo_json_str($state,$message,$url); die;
        }

        if ($data['status']==2 && $data['remark']=='') {
            $state = 'Please enter a reason to refuse';
            echo $this->echo_json_str($state,$message,$url); die;
        }
               
        $list = $obj->where($map)->save($data);

        if($data['status']==1){
            $content=$info["toName"]." has agreed to your appointment.time:".date("F,d,Y",$info['time']);
            $title=$info["toName"]." has agreed to your apprentice.";
        }else{
            $content=$info["toName"]." has rejected your appointment.time:".date("F,d,Y",$info['time']).";reason:".$data['remark'];
            $title=$info["toName"]." has rejected to your apprentice.";
        }

        $this->sendSysMsg($info['fromID'],$title,$content); 

        if ($data['status']==1){ //如果同意则添加到培训表
            //添加到培训详情表
            unset($data);
            unset($map);
            $data['menteesID'] = $info['fromID'];
            $data['mentorsID'] = $info['toID'];
            $data['trainItem'] = $info['trainItem'];
            $data['skills'] = $info['skills'];
            $data['train'] = $info['train'];
            $data['target'] = $info['target'];
            $data['work'] = $info['work'];
            $data['createTime'] = time();
            $data['work'] = $info['work'];

            $map['menteesID'] = $info['fromID'];
            $map['mentorsID'] = $info['toID'];
            $map['trainItem'] = $info['trainItem'];
            $list = M('Train')->where($map)->find();
            if (!$list) {
                M('Train')->add($data);
            }            
        }
        $state = 'SUCCESS';
        $message = L('_action_success');
        $url = U('Apprentice/index');
        echo $this->echo_json_str($state,$message,$url);
    }

}